﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;

namespace MailSender
{
    public class Mail
    {
        public string SendFrom { get; set; }
        public string Sender { get; set; }
        public string Subject { get; set; }
        public string SendTo { get; set; }
        public string Content { get; set; }

        public bool Send()
        {
            try
            {
                MailMessage msg = new MailMessage();
                msg.To.Add(this.SendTo);
                msg.From = new MailAddress(this.SendFrom, this.Sender, System.Text.Encoding.UTF8);
                msg.Subject = this.Subject;
                msg.SubjectEncoding = System.Text.Encoding.UTF8;
                msg.Body = this.Content;
                msg.BodyEncoding = System.Text.Encoding.UTF8;
                msg.IsBodyHtml = true;
                msg.Priority = MailPriority.Normal;

                SmtpClient client = new SmtpClient();
                client.Host = "localhost";
                client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

                // 异步发送，不管是否成功，立即返回
                //object userState = msg;
                //client.SendAsync(msg, userState); 

                client.Send(msg);
                //MessageBox.Show("发送成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch //(SmtpException ex)
            {
                return false;
                //MessageBox.Show(ex.Message, "发送邮件出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return true;
        }
    }
}
